;
(function() {
	var form = $('#menuForm'), menuid, text, parentId, operate;
	
	form.validate({
		errorElement: 'span',
		focusInvalid: true,
		messages: {
		},
		rules: {
			text: {
				minlength: 2,
				maxlength: 30,
				required: true
			},
			type: {
				required: true
			},
			href: {
				required: true
			},
			sort: {
				required: true,
				number: true
			},
			state: {
				required: true
			},
			icon: {
				required: true
			}
		},
		submitHandler: function(form) {
			submit(operate);
		}
	});
	_AJAX({
		url: AJAX_URL_USER + 'api/sys/sysMenu/list',
		type: 'get',
		success: function(data) {
			$("#resTree").jstree({
				"core": {
					"animation": 0,
					"themes": {
						theme: "classic",
						"dots": true,
						"icons": true
					},
					"check_callback": true,
					'data': data.data
				},
				"types": {
					"default": {
						"valid_children": ["default", "file"]
					}
				},
				"plugins": ["wholerow"]
			}).on("select_node.jstree", function(node, selectd) {
		        menuid = selectd.node.id;
		        var parents = selectd.node.parents;

				_AJAX({
					url: AJAX_URL_USER + 'api/sys/sysMenu/select/' + menuid,
					type: 'get',
					success: function(data) {
						$("span.error").remove();
						$('#menuForm :input').each(function (a) {
	                        $(this).removeClass("error");
	                    });

	                    data = data.data;
	                    $('input[name=text]').val(data.text);
	                    $('input[name=icon]').val(data.icon);
	                    $('input[name=sort]').val(data.sort);
	                    $('input[name=href]').val(data.href);
	                    $("select[name=type]").val(data.type);
	                    $("select[name=state]").val(data.situation);
	
	                    $('#menuForm :input').each(function (a) {
	                        $(this).attr('disabled', "disabled");
	                        $(this).attr("readonly", "true");
	                    });
	                    
	                    if(parents.length == 3) {
		                    $('.btn-children').attr("disabled", true);
				        }
	                    else {
	                    	$('.btn-children').attr("disabled", false);
	                    }
	
	                    $('.btn-parent').attr("disabled",true);
	                    $('.btn-edit').attr("disabled",false);
	                    $('.btn-delete').attr("disabled",false);
	                    $('.btn-disable').attr("disabled",false);
					}
				});
			});
		}
	});

	$('.btn-parent').click(function() {
		operate = "parent";
		parentId = -1;
		$('#menuForm :input').each(function(a) {
        	$(this).val("");
            $(this).attr("disabled", false);
            $(this).attr("readonly", false);
		});
        $('.btn-edit').attr('disabled', "disabled");
        $('.btn-delete').attr('disabled', "disabled");
        $('.btn-disable').attr('disabled', "disabled");
        $('.btn-children').attr('disabled', "disabled");
	});
    $('.btn-children').click(function () {
		operate = "children";
		parentId = menuid;
        $('#menuForm :input').each(function (a) {
        	$(this).val("");
            $(this).attr("disabled", false);
            $(this).attr("readonly", false);
        });
        $('.btn-edit').attr('disabled', "disabled");
        $('.btn-delete').attr('disabled', "disabled");
        $('.btn-disable').attr('disabled', "disabled");
        $('.btn-parent').attr('disabled', "disabled");
    });
    $('.btn-edit').click(function () {
    	operate = "edit";
        $('#menuForm :input').each(function (a) {
            $(this).attr('disabled', false);
            $(this).attr("readonly", false);
        });
    });
    $('.btn-cancel').click(function () {
//		$("span.error").remove();
//      $('#menuForm :input').each(function (a) {
//      	$(this).val("");
//          $(this).attr('disabled', "disabled");
//          $(this).attr("readonly", "true");
//          $(this).removeClass("error");
//      });
//      $(".portlet-title,button").attr("disabled", "disabled");

        window.location.reload();
    });
    $('.btn-delete').click(function () {
        if (menuid){
            layer.confirm('确认要删除此菜单及其下级所有资源吗？', {
                btn: ['确定', '取消']
            }, function () {
				_AJAX({
					url: AJAX_URL_USER + 'api/sys/sysMenu/delete/'+menuid,
					type: 'get',
					successmsg: true,
					success: function (r) {
                        window.location.reload();
                    }
				});
            })
        }
    });
    
    function submit(operate) {
    	var _url = null;
	    var text = $('input[name=text]').val();
	    var icon = $('input[name=icon]').val();
	    var sort = $('input[name=sort]').val();
	    var href = $('input[name=href]').val();
	    var state = $('select[name=state]').val();
	    var type = $('select[name=type]').val();

    	var data = {
    		id: menuid,
    		text: text,
    		icon: icon,
    		sort: sort,
    		href: href,
    		parentId: parentId,
    		type: type,
    		situation: state
    	}
    	
    	switch(operate) {
			case "edit":
				_url = AJAX_URL_USER + 'api/sys/sysMenu/update';
		  		break;
			case "children":
				_url = AJAX_URL_USER + 'api/sys/sysMenu/insert';
			  break;
			case "parent":
				_url = AJAX_URL_USER + 'api/sys/sysMenu/insert';
			  break;
		}
    	
		_AJAX({
			url: _url,
			type: 'post',
			data: JSON.stringify(data),
			successmsg: true,
			success: function (r) {
                window.location.reload();
            }
		});
    }
    
    $('.btn-disable').click(function () {
        if (menuid){
            layer.confirm('确认要禁用此菜单及其下级所有资源吗？', {
                btn: ['确定', '取消']
            }, function () {
				_AJAX({
					url: AJAX_URL_USER + 'api/sys/sysMenu/disable/'+menuid,
					type: 'get',
					successmsg: true,
					success: function (r) {
                        window.location.reload();
                    }
				});
            })
        }
    });
    
    $("#icon_add").click(function() {
    	$('#icon_add_div').modal('show');
    });
}());
